terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "4.37.0"
    }
  }
}


provider "google" {
  credentials = file(var.credentials_file)
  project     = var.project
  region      = var.region
  zone        = var.zone
}

resource "google_compute_network" "vpc_network" {
  name = "jp-test-network"
}

resource "google_compute_instance" "vm_instance" {
  name         = "jp-test-instance"
  machine_type = "f1-micro"
  tags         = ["dev"]

  boot_disk {
    initialize_params {
      //image = "debian-cloud/debian-11"
      image = "ubuntu-os-pro-cloud/ubuntu-pro-2204-lts"
    }
  }

  network_interface {
    network = google_compute_network.vpc_network.name
    access_config {
    }
  }
  metadata  = {
    user-data= "${data.template_file.user_data.rendered}"
  }
}

data "template_file" "user_data" {
  template = templatefile("../aws/scripts/add-ssh-web-app.yaml",
  {
        Signature: var.Signature
        ManagerIp: var.ManagerIp
        //ssh_key  : var.ssh_key
        //Signature: "BOI8PrXNaVANosmOgylvmCMpL7Ek3AEAmvkrAJJ6fKxoJQTh1w0Tksv3uvXAVsxmfXeKE97il70Q8blrvu5rAjNuZXRzZWRnZS5pbw==",
        //ManagerIp: 	"5.161.133.68",
        ssh_public_key_file:  "../tf-cloud-init.pub" ,
        ssh_key : file("${path.module}/../tf-cloud-init.pub")
  })
}

resource "google_compute_firewall" "ssh" {
  name = "test-firewall"
  network = google_compute_network.vpc_network.name
  allow {
    protocol = "icmp"
  }
  allow {
    protocol = "tcp"
    ports = ["22"]
  }
  source_tags = ["test-firewall-ssh"]
  source_ranges = ["0.0.0.0/0"]
}


//resource "google_os_login_ssh_public_key" "cache" {
  //user = "3nets"
  //key = file("../tf-cloud-init.pub")
//}
